package MyListener;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import MyInterface.NewPage;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * Created by dell on 2017/3/23.
 */

public class RecycleListener extends RecyclerView.OnScrollListener implements NewPage {

    //滑动状态
    private int scrollState = 0;
    //最后一个item的位置
    private int lastItemposition = 0;
    //最后一个的位置
    private int[] lastpositions;

    private int a = 0;

    private List list = new ArrayList();

    public RecycleListener(List list) {
        this.list = list;
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        try{
            super.onScrollStateChanged(recyclerView, newState);
            scrollState = newState;
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            int visibleItemCount = layoutManager.getChildCount();
            int totalItemCount = layoutManager.getItemCount();
            if ((visibleItemCount > 0 && scrollState == RecyclerView.SCROLL_STATE_IDLE)
                    && !recyclerView.canScrollVertically(1)) {
                setNewPage(recyclerView);
//            Toast.makeText(recyclerView.getContext(),"this is last",Toast.LENGTH_SHORT).show();
            }
        }catch (Exception e){
            Log.d(TAG, "onScrollStateChanged: ");
        }
    }

        @Override
        public void onScrolled (RecyclerView recyclerView,int dx, int dy){
            try {
                super.onScrolled(recyclerView, dx, dy);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                lastItemposition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            }catch (Exception e){
                Log.d(TAG, "onScrolled: ");
            }
        }


        @Override
        public void setNewPage (View view){
        }
    }
